import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class RockPaperScissorsGame extends JFrame {
    private JLabel playerChoiceLabel;
    private JLabel computerChoiceLabel;
    private JLabel resultLabel;
    private JButton rockButton;
    private JButton paperButton;
    private JButton scissorsButton;
    
    public RockPaperScissorsGame() {
        // 设置窗口标题和大小
        setTitle("猜拳游戏 - 石头剪刀布");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        
        // 创建主面板
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout(10, 10));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        
        // 创建选择面板
        JPanel choicePanel = new JPanel();
        choicePanel.setLayout(new GridLayout(3, 1, 10, 10));
        
        // 创建按钮
        rockButton = new JButton("石头");
        paperButton = new JButton("布");
        scissorsButton = new JButton("剪刀");
        
        // 添加按钮到选择面板
        choicePanel.add(rockButton);
        choicePanel.add(paperButton);
        choicePanel.add(scissorsButton);
        
        // 创建显示面板
        JPanel displayPanel = new JPanel();
        displayPanel.setLayout(new GridLayout(3, 1, 10, 10));
        
        playerChoiceLabel = new JLabel("你的选择: 等待选择", SwingConstants.CENTER);
        computerChoiceLabel = new JLabel("电脑选择: 等待选择", SwingConstants.CENTER);
        resultLabel = new JLabel("游戏结果: 请做出你的选择", SwingConstants.CENTER);
        
        displayPanel.add(playerChoiceLabel);
        displayPanel.add(computerChoiceLabel);
        displayPanel.add(resultLabel);
        
        // 添加面板到主面板
        mainPanel.add(choicePanel, BorderLayout.WEST);
        mainPanel.add(displayPanel, BorderLayout.CENTER);
        
        // 添加主面板到窗口
        add(mainPanel);
        
        // 添加按钮事件监听器
        rockButton.addActionListener(new GameActionListener("石头"));
        paperButton.addActionListener(new GameActionListener("布"));
        scissorsButton.addActionListener(new GameActionListener("剪刀"));
    }
    
    // 内部类处理游戏逻辑
    private class GameActionListener implements ActionListener {
        private String playerChoice;
        
        public GameActionListener(String choice) {
            this.playerChoice = choice;
        }
        
        @Override
        public void actionPerformed(ActionEvent e) {
            // 玩家选择
            playerChoiceLabel.setText("你的选择: " + playerChoice);
            
            // 电脑随机选择
            String[] choices = {"石头", "剪刀", "布"};
            String computerChoice = choices[new Random().nextInt(choices.length)];
            computerChoiceLabel.setText("电脑选择: " + computerChoice);
            
            // 判断胜负
            String result = determineWinner(playerChoice, computerChoice);
            resultLabel.setText("游戏结果: " + result);
        }
        
        private String determineWinner(String player, String computer) {
            if (player.equals(computer)) {
                return "平局!";
            }
            
            switch (player) {
                case "石头":
                    return computer.equals("剪刀") ? "你赢了!" : "电脑赢了!";
                case "剪刀":
                    return computer.equals("布") ? "你赢了!" : "电脑赢了!";
                case "布":
                    return computer.equals("石头") ? "你赢了!" : "电脑赢了!";
                default:
                    return "无效的选择";
            }
        }
    }
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            RockPaperScissorsGame game = new RockPaperScissorsGame();
            game.setVisible(true);
        });
    }
}